//
//  RSPictureView.swift
//  RSWeibo
//
//  Created by Macx on 16/8/15.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit

//  重用标记
private let StatusPictureViewCellIdentifier = "StatusPictureViewCellIdentifier"

//  当前屏幕的宽度
let ScreenWidth = UIScreen.mainScreen().bounds.size.width
//  当前屏幕的高度
let ScreenHeight = UIScreen.mainScreen().bounds.size.height

//  每项之间间距
private let itemMargin: CGFloat = 5

// 没个 视图的宽度
private let itemWidth = (ScreenWidth - 2*itemMargin - 2*statusSubViewMargin) / 3

class RSPictureView: UICollectionView {
    
    // 显示 配图的 数据源
    var picUrls: [RSPicUrlModel]?{
    
        didSet {
        
            countLable.text = "\(picUrls?.count ?? 0)"
         
        
            // 根据配图个数 计算大小
            let size = itemsSizeWithCount(picUrls?.count ?? 0)
            
            // 更新约束
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            
            // 刷新数据
            self.reloadData()
        
        }
    
    }

    
    // 显示配图个数的 label
    private lazy var countLable = UILabel(fontSize: 20, textColor: UIColor.whiteColor())
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        // 创建一个布局, 不使用外界传进来的 布局
        let flowLayout = UICollectionViewFlowLayout()
        
        // 设置大小
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth)
        // 水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
        // 垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
        
        
        //  注册cell
        registerClass(RSPictureViewCell.self, forCellWithReuseIdentifier: StatusPictureViewCellIdentifier)
        
        // 代理
        dataSource = self
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    // 通过个数计算 配图的大小
    private func itemsSizeWithCount(count: Int) -> CGSize{
    
        //  列数
        let cols = count > 3 ? 3 : count
        //  行数
        let rows = (count - 1) / 3 + 1
        
//        print("行数 列数", cols, rows)
        
        
        //  计算配图的大小
        //  宽度
        let currentWidth = itemWidth * CGFloat(cols) + CGFloat(cols - 1) * itemMargin
        //  高度
        let currentHeight = itemWidth * CGFloat(rows) + CGFloat(rows - 1) * itemMargin
        
        
        return CGSize(width: currentWidth, height: currentHeight)
    
    }
    
    
    
    private func setupUI(){
    
        backgroundColor = UIColor.greenColor()
        
        
        // 把显示个数的 label添加进来
        addSubview(countLable)
        
        countLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        
        
    }

}


extension RSPictureView: UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StatusPictureViewCellIdentifier, forIndexPath: indexPath) as! RSPictureViewCell
        
        // 设置数据源
        cell.picInfo = picUrls![indexPath.item]
        
        return cell
    }
    
}


class RSPictureViewCell: UICollectionViewCell {
    
    
    
    //  绑定数据
    var picInfo: RSPicUrlModel? {
        didSet {
            if let url = picInfo?.thumbnail_pic {
                imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
                //  判断gif图片是否显示
                gifImageView.hidden = !url.hasSuffix(".gif")
                
                
                
            }
        }
    }
    
    //  MARK:   --懒加载控件
    private lazy var imageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        //  修改显示模型为等比填充,保证图片比例不变
        view.contentMode = UIViewContentMode.ScaleAspectFill
        //  多余图片剪切掉
        view.clipsToBounds = true
        return view
        
    }()
    //  gif图片视图
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        //  添加控件
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        //  设置约束
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    }

    
    
}



























